package JavaSE.CODE.javaNet.File;

import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;

public class TcpServerDemo {
    public static void main(String[] args) throws Exception{
        //如果一个抛出体内有很多个异常，会怎样

        ServerSocket serverSocket = new ServerSocket(9000);
        //监听客户端连接（阻塞式监听，会一直等待） 类似scanner.next

        Socket socket = serverSocket.accept();

        //获取输入流
        InputStream is = socket.getInputStream();

        FileOutputStream fileOutputStream = new FileOutputStream("tteesstt.txt");

        byte[] buffer = new byte[1024];
        int len;
        while((len=is.read(buffer))>0){
            fileOutputStream.write(buffer,0,len);
            //直接从文件角度复制，不用担心乱码问题
        }

        //通知客户端我接收到了
        OutputStream outputStream = socket.getOutputStream();
        //套接字写，写到相连的套接字中去了
        outputStream.write("我接收完毕了".getBytes());


        fileOutputStream.close();
        is.close();
        socket.close();
        serverSocket.close();
    }
}
